﻿!include FileFunc.nsh
!include LogicLib.nsh
!include x64.nsh

!define WEASEL_VERSION 0.9.13
!define WEASEL_BUILD ${WEASEL_VERSION}.2

!define WEASEL_ROOT $INSTDIR\weasel-${WEASEL_VERSION}
!define /date CUR_TIME "%Y%m%d_%H%M" ;%S

Name "小狼毫（吳語定製版） ${WEASEL_BUILD}"
OutFile "archives\weasel-ng-${WEASEL_BUILD}-installer.exe"
Icon ng.ico
SetCompressor /SOLID lzma

VIProductVersion "${WEASEL_BUILD}"
VIAddVersionKey /LANG=2052 "ProductName" "小狼毫輸入法（吳語定製版）"
VIAddVersionKey /LANG=2052 "Comments" "Powered by RIME | 中州韻輸入法引擎"
VIAddVersionKey /LANG=2052 "CompanyName" "Wu-Chinese.com"
VIAddVersionKey /LANG=2052 "LegalCopyright" "Copyleft Wu-Chinese.com 2012"
VIAddVersionKey /LANG=2052 "FileDescription" "小狼毫輸入法（吳語定製版）"
VIAddVersionKey /LANG=2052 "FileVersion" "${WEASEL_VERSION}"

InstallDir $PROGRAMFILES\Rime
InstallDirRegKey HKLM "Software\Rime\Weasel" "InstallDir"

RequestExecutionLevel admin
LoadLanguageFile "${NSISDIR}\Contrib\Language Files\SimpChinese.nlf"

;--------------------------------
;Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------
Function .onInit
  MessageBox MB_OK|MB_ICONINFORMATION "小狼豪（吳語定製版）目前與原版小狼豪無法共存，如果電腦上已經安裝有原版小狼豪，則可能會提示卸載。特此提醒。"
  ReadRegStr $R0 HKLM \
  "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel" \
  "UninstallString"
  StrCmp $R0 "" done
 
  MessageBox MB_OKCANCEL|MB_ICONQUESTION \
  "安裝前需要先卸載舊版本的小狼毫（原版或吳語定製版）。$\n$\n按下「確定」卸載舊版本，按下「取消」放棄本次安裝。" \
  IDOK uninst
  Abort
uninst:
  Exec '$R0 /S'
done:
FunctionEnd

; The stuff to install
Section "Weasel"

  SectionIn RO
  
  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\Rime\Weasel "InstallDir" "$INSTDIR"

  StrCpy $INSTDIR "${WEASEL_ROOT}"

  IfFileExists "$INSTDIR\WeaselServer.exe" 0 +2
  ExecWait '"$INSTDIR\WeaselServer.exe" /quit'

  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  ; program files
  File "LICENSE.txt"
  File "README.txt"
  File "weasel.ime"
  File "weaselx64.ime"
  File "weaselt.ime"
  File "weaseltx64.ime"
  File "WeaselDeployer.exe"
  File "WeaselServer.exe"
  File "opencc.dll"
  File "WinSparkle.dll"
  File "zlib1.dll"
  nsExec::ExecToLog 'cmd /c mklink /h Settings.exe WeaselDeployer.exe'
  ; shared data files
  SetOutPath  $INSTDIR\data
  File "data\default.yaml"
  File "data\weasel.yaml"
  File "data\essay.kct"
  File "data\pehlih_*.schema.yaml"
  File "data\pehlih_*.dict.yaml"
  File "data\luna_*.schema.yaml"
  File "data\luna_*.dict.yaml"
  ; appdata files
  SetOutPath  $APPDATA\Rime
  SetOverwrite off
  File /r "appdata\luna*.yaml"
  SetOverwrite on
  ;File /r "appdata\luna*.bin"

  ; opencc data files
  SetOutPath  $INSTDIR\data\opencc
  File "data\opencc\*.ini"
  File "data\opencc\*.ocd"
  File "data\opencc\*.txt"
  ; images
  SetOutPath  $INSTDIR\data\preview
  File "data\preview\*.png"

  SetOutPath $INSTDIR
  
  ; test /T flag for zh_TW locale
  StrCpy $R2  "weasel"
  ${GetParameters} $R0
  ClearErrors
  ${GetOptions} $R0 "/T" $R1
  IfErrors +2 0
  StrCpy $R2 "weaselt"

  ${If} ${RunningX64}
    ExecWait 'rundll32 "$INSTDIR\$R2x64.ime" install'
  ${Else}
    ExecWait 'rundll32 "$INSTDIR\$R2.ime" install'
  ${EndIf}

  ; run as user...
  ExecWait "$INSTDIR\WeaselDeployer.exe /deploy"

  WriteRegStr HKLM SOFTWARE\Rime\Weasel "Special" "Ng"

  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel" "DisplayName" "小狼毫輸入法（吳語定製版）"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel" "NoRepair" 1
  WriteUninstaller "$INSTDIR\uninstall.exe"
  
SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\小狼毫輸入法（吳語定製版）"
  CreateShortCut "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\說明書.lnk" "$INSTDIR\README.txt"
  CreateShortCut "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\輸入法設定.lnk" "$INSTDIR\Settings.exe" "" "$INSTDIR\Settings.exe" 0
  CreateShortCut "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\用戶詞典管理.lnk" "$INSTDIR\Settings.exe" "/dict" "$INSTDIR\WeaselDeployer.exe" 0
  CreateShortCut "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\小狼毫算法服務.lnk" "$INSTDIR\WeaselServer.exe" "" "$INSTDIR\WeaselServer.exe" 0
  CreateShortCut "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\卸載小狼毫.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  
SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"
  
  ExecWait '"$INSTDIR\WeaselServer.exe" /quit'

  ${If} ${RunningX64}
    ExecWait 'rundll32 "$INSTDIR\weaselx64.ime" uninstall'
  ${Else}
    ExecWait 'rundll32 "$INSTDIR\weasel.ime" uninstall'
  ${EndIf}
  
  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"
  DeleteRegKey HKLM SOFTWARE\Rime

  ; Remove files and uninstaller
  SetOutPath $TEMP
  Delete "$INSTDIR\data\opencc\*.*"
  Delete "$INSTDIR\data\preview\*.*"
  Delete "$INSTDIR\data\*.*"
  Delete "$INSTDIR\*.*"
  RMDir "$INSTDIR\data\opencc"
  RMDir "$INSTDIR\data\preview"
  RMDir "$INSTDIR\data"
  RMDir "$INSTDIR"
  Delete "$APPDATA\Rime\luna_*.bin"
  Delete "$APPDATA\Rime\luna_*.schema.yaml"
  Delete "$SMPROGRAMS\小狼毫輸入法（吳語定製版）\*.*"
  RMDir "$SMPROGRAMS\小狼毫輸入法（吳語定製版）"

SectionEnd
